#include "restful_diagnosis.h"

#include <algorithm>
#include <future>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>

#include "utils_file_ex.h"
#include "utils_string_ex.h"
#include "utils_string_ex.h"
#include "utils_log.h"
#include "common_shell.h"
#include "ws_protocol_service.h"
#include "ws_connection_manager.h"
#include "common_property.h"

namespace El {
namespace Restful {

RestfulDiagnosis &RestfulDiagnosis::GetInstance()
{
    static RestfulDiagnosis instance;
    return instance;
}

bool RestfulDiagnosis::Start()
{
    Common::Shell::GetInstance().RegisterCommand("protocol",
                                                 std::bind(&RestfulDiagnosis::Protocol, this, std::placeholders::_1));
    return true;
}

void RestfulDiagnosis::Protocol(const std::string &args)
{
    std::vector<std::string> vec_args;
    Utils::Split(args, " ", vec_args);
    if (vec_args.size() < 1) {
        EL_ERROR("Invalid args: {}", args.c_str());
        return;
    }

    if (vec_args[0] == "auth") {
        if (vec_args.size() < 2) {
            EL_ERROR("Invalid auth command, usage: protocol auth [on|off]");
            return;
        }

        if (vec_args[1] == "off") {
            EL_INFO("Auth is off");
            Common::CommonProperty::GetInstance().SetProperty("protocol.auth", "off");
        } else if (vec_args[1] == "on") {
            EL_INFO("Auth is on");
            Common::CommonProperty::GetInstance().SetProperty("protocol.auth", "on");
        } else {
            EL_ERROR("Invalid auth option: {}, should be 'on' or 'off'", vec_args[1]);
        }
    } else {
        EL_ERROR("Invalid command: {}", vec_args[0]);
    }
}

} // namespace Restful
} // namespace El